package com.agileai.crm.controller.procustomer;

import java.util.Date;

import com.agileai.crm.bizmoduler.procustomer.OrgInfoManage;
import com.agileai.crm.common.PrivilegeHelper;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

public class OrgInfoManageEditHandler extends StandardEditHandler {
	public OrgInfoManageEditHandler() {
		super();
		this.listHandlerClass = OrgInfoManageListHandler.class;
		this.serviceId = buildServiceId(OrgInfoManage.class);
	}

	public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (isReqRecordOperaType(operaType)) {
			DataRow record = getService().getRecord(param);
			this.setAttributes(record);
		}
		if ("insert".equals(operaType)) {
			setAttribute("doEdit8Save", true);
		}
		if ("update".equals(operaType) || "detail".equals(operaType)) {
			DataRow record = getService().getRecord(param);
			if (record != null) {
				User user = (User) this.getUser();
				PrivilegeHelper privilegeHelper = new PrivilegeHelper(user);
				
				if (privilegeHelper.isInfoCollector() ||privilegeHelper.isTelNetSalesman() || privilegeHelper.isSalesDirector()){
					setAttribute("doEdit8Save", true);					
				}
				if (record.get("ORG_STATE").equals("0")) {
					if (privilegeHelper.isTelNetSalesman() || privilegeHelper.isSalesDirector()){
						setAttribute("createClue", true);
						setAttribute("doRevokeApporve", true);
					}
				} else {
					if (privilegeHelper.isTelNetSalesman() || privilegeHelper.isSalesDirector()){
						setAttribute("doApporve", true);
					}
				}
			}
			this.setAttributes(record);
		}

		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}

	protected void processPageAttributes(DataParam param) {
		setAttribute("ORG_STATE", FormSelectFactory.create("PER_STATE")
				.addSelectedValue(getOperaAttributeValue("ORG_STATE", "0")));
		setAttribute("ORG_TYPE", FormSelectFactory.create("ORG_TYPE")
				.addSelectedValue(getOperaAttributeValue("ORG_TYPE", "")));
		User user = (User) this.getUser();
		this.setAttribute("ORG_CREATER_NAME",
				this.getAttribute("ORG_CREATER_NAME", user.getUserName()));
		this.setAttribute("ORG_CREATER",
				this.getAttribute("ORG_CREATER", user.getUserId()));
		String pcrtDate = (String) this.getAttribute("ORG_CREATE_TIME",
				DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL,
						new Date()));
		if (this.getAttribute("ORG_CREATE_TIME") == null) {
			this.setAttribute("ORG_CREATE_TIME", pcrtDate);
		}
	}

	@SuppressWarnings("unused")
	public ViewRenderer doSaveAction(DataParam param) {
		String operateType = param.get(OperaType.KEY);
		String responseText = SUCCESS;
		String orgEmail = param.get("ORG_EMAIL");
		if (OperaType.CREATE.equals(operateType)) {
			if (orgEmail != "") {
				DataParam repeatParam = new DataParam("ORG_EMAIL", orgEmail);
				DataRow record = getService().getRecord(repeatParam);
				if (record != null && record.size() > 0) {
					responseText = "repeat";
				} else {
					getService().createRecord(param);
				}
			} else {
				getService().createRecord(param);
			}
		} else if (OperaType.UPDATE.equals(operateType)) {
			if (orgEmail == "") {
				getService().updateRecord(param);
			} else {
				DataParam repeatParam = new DataParam("ORG_EMAIL", orgEmail);
				DataRow record = getService().getRecord(repeatParam);
				if (record != null) {
					if (record.getString("ORG_ID").equals(param.get("ORG_ID"))) {
						getService().updateRecord(param);
					}
				} else {
					if (record != null && record.size() > 0) {
						responseText = "repeat";
					} else {
						getService().updateRecord(param);
					}
				}
			}
		}
		return new AjaxRenderer(responseText);
	}

	@PageAction
	public ViewRenderer doCreateClueAction(DataParam param) {
		storeParam(param);
		String a = "OrgInfoCreateClueEdit";
		return new DispatchRenderer(getHandlerURL(a) + "&" + OperaType.KEY
				+ "=doCreateClueAction&comeFrome=doCreateClueAction");
	}

	@PageAction
	public ViewRenderer doApporve(DataParam param) {
		param.put("ORG_STATE", "0");
		getService().changeStateRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}

	@PageAction
	public ViewRenderer doRevokeApporve(DataParam param) {
		param.put("ORG_STATE", "1");
		getService().changeStateRecord(param);
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}

	protected OrgInfoManage getService() {
		return (OrgInfoManage) this.lookupService(this.getServiceId());
	}
}
